package com.cnbot.aimolib;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author ytf
 * Created by on 2021/06/08 09:46
 */
public class AvatarBox extends View {

    private Paint mPaint;
    private int strokeLen;
    private int strokeWidth;
    private int strokeColor;

    private float widthRatio = 1.0f, heightRatio = 1.0f;

    public AvatarBox(Context context) {
        this(context, null);
    }

    public AvatarBox(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AvatarBox(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AvatarBox);
        strokeColor = ta.getColor(R.styleable.AvatarBox_ab_stroke_color, Color.WHITE);
        strokeWidth = ta.getDimensionPixelOffset(R.styleable.AvatarBox_ab_stroke_width, -1);
        strokeLen = ta.getDimensionPixelOffset(R.styleable.AvatarBox_ab_stroke_len, -1);

        ta.recycle();

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(strokeColor);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    public void config(int len, int width, int color){
        strokeColor = color;
        strokeWidth = width;
        strokeLen = len;
        mPaint.setColor(strokeColor);
        mPaint.setStrokeWidth(strokeWidth);
        postInvalidate();
    }

    public void setRatio(float widRatio, float heiRatio){
        widthRatio = widRatio;
        heightRatio = heiRatio;
        postInvalidate();
    }

    public RectF getRect(){
        return new RectF(leftAnchor, topAnchor, rightAnchor, bottomAnchor);
    }

    private int leftAnchor;
    private int topAnchor;
    private int rightAnchor;
    private int bottomAnchor;
    private Rect boxRect = new Rect();

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        int displayWid = (int) (w * widthRatio);
        int displayHei = (int) (h * heightRatio);

        if(strokeWidth == -1){
            strokeWidth = Math.min(displayWid, displayHei)/30;
            mPaint.setStrokeWidth(strokeWidth);
        }
        int half = strokeWidth/2;
        leftAnchor = (w - displayWid)/2 + half;
        topAnchor = (h - displayHei)/2 + half;
        rightAnchor = leftAnchor + displayWid;
        bottomAnchor = topAnchor + displayHei;

        if(strokeLen == -1){
            strokeLen = Math.min(displayWid, displayHei) / 10;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(leftAnchor, topAnchor, leftAnchor, topAnchor + strokeLen, mPaint);
        canvas.drawLine(leftAnchor, topAnchor, leftAnchor + strokeLen, topAnchor, mPaint);

        canvas.drawLine(rightAnchor - strokeLen, topAnchor, rightAnchor, topAnchor, mPaint);
        canvas.drawLine(rightAnchor, topAnchor, rightAnchor, topAnchor + strokeLen, mPaint);

        canvas.drawLine(rightAnchor, bottomAnchor-strokeLen, rightAnchor, bottomAnchor, mPaint);
        canvas.drawLine(rightAnchor, bottomAnchor, rightAnchor-strokeLen, bottomAnchor, mPaint);

        canvas.drawLine(leftAnchor, bottomAnchor-strokeLen, leftAnchor, bottomAnchor, mPaint);
        canvas.drawLine(leftAnchor, bottomAnchor, leftAnchor+strokeLen, bottomAnchor, mPaint);
    }
}
